home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / WinCE / SDKWindowsCE / HandHeldPCPro30 / sdk.exe / Jupiter SDK / data1.cab / MFC / src / viewprnt.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-19  |  12.0 KB  |  415 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10.  
  11. #include "stdafx.h"
  12.  
  13. #if !defined(_WIN32_WCE_NO_PRINTING)
  14. #ifdef AFX_PRINT_SEG
  15. #pragma code_seg(AFX_PRINT_SEG)
  16. #endif
  17.  
  18. #ifdef _DEBUG
  19. #undef THIS_FILE
  20. static char THIS_FILE[] = __FILE__;
  21. #endif
  22.  
  23. #if defined(_WIN32_WCE)
  24. #define hDC hdc // the print dialog data structure renames this member
  25. #endif // _WIN32_WCE
  26. /////////////////////////////////////////////////////////////////////////////
  27. // Printing Dialog
  28.  
  29. class CPrintingDialog : public CDialog
  30. {
  31. public:
  32.     //{{AFX_DATA(CPrintingDialog)
  33.     enum { IDD = AFX_IDD_PRINTDLG };
  34.     //}}AFX_DATA
  35.     CPrintingDialog::CPrintingDialog(CWnd* pParent)
  36.         {
  37.             Create(CPrintingDialog::IDD, pParent);      // modeless !
  38.             _afxWinState->m_bUserAbort = FALSE;
  39.         }
  40.     virtual ~CPrintingDialog() { }
  41.  
  42.     virtual BOOL OnInitDialog();
  43.     virtual void OnCancel();
  44. };
  45.  
  46. BOOL CALLBACK _AfxAbortProc(HDC, int)
  47. {
  48.     _AFX_WIN_STATE* pWinState = _afxWinState;
  49.     MSG msg;
  50.     while (!pWinState->m_bUserAbort &&
  51.         ::PeekMessage(&msg, NULL, NULL, NULL, PM_NOREMOVE))
  52.     {
  53.         if (!AfxGetThread()->PumpMessage())
  54.             return FALSE;   // terminate if WM_QUIT received
  55.     }
  56.     return !pWinState->m_bUserAbort;
  57. }
  58.  
  59. BOOL CPrintingDialog::OnInitDialog()
  60. {
  61.     SetWindowText(AfxGetAppName());
  62.     CenterWindow();
  63.     return CDialog::OnInitDialog();
  64. }
  65.  
  66. void CPrintingDialog::OnCancel()
  67. {
  68.     _afxWinState->m_bUserAbort = TRUE;  // flag that user aborted print
  69.     CDialog::OnCancel();
  70. }
  71.  
  72. /////////////////////////////////////////////////////////////////////////////
  73. // CView printing commands
  74.  
  75. #if defined(_WIN32_WCE)
  76. BOOL CView::DoPreparePrinting(CPrintInfo* pInfo)
  77. {
  78.     ASSERT(pInfo != NULL);
  79.     ASSERT(pInfo->m_pPD != NULL);
  80.  
  81.     // don't prompt the user if we're doing print preview, printing directly,
  82.     // or printing via IPrint and have been instructed not to ask
  83.     CWinApp* pApp = AfxGetApp();
  84.  
  85.     // preset From-To range same as Min-Max range
  86.     pInfo->m_pPD->m_nFromPage = (WORD)pInfo->GetMinPage();
  87.     pInfo->m_pPD->m_nToPage = (WORD)pInfo->GetMaxPage();
  88.  
  89.     if (pApp->DoPrintDialog(pInfo->m_pPD) != IDOK)
  90.         return FALSE;       // do not print
  91.     ASSERT(pInfo->m_pPD->m_pd.hDC != NULL);
  92.  
  93.     VERIFY(pInfo->m_strPageDesc.LoadString(AFX_IDS_PREVIEWPAGEDESC));
  94.     return TRUE;
  95. }
  96. #else // _WIN32_WCE
  97. BOOL CView::DoPreparePrinting(CPrintInfo* pInfo)
  98. {
  99.     ASSERT(pInfo != NULL);
  100.     ASSERT(pInfo->m_pPD != NULL);
  101.  
  102.     if (pInfo->m_pPD->m_pd.nMinPage > pInfo->m_pPD->m_pd.nMaxPage)
  103.         pInfo->m_pPD->m_pd.nMaxPage = pInfo->m_pPD->m_pd.nMinPage;
  104.  
  105.     // don't prompt the user if we're doing print preview, printing directly,
  106.     // or printing via IPrint and have been instructed not to ask
  107.  
  108.     CWinApp* pApp = AfxGetApp();
  109.     if (pInfo->m_bPreview || pInfo->m_bDirect ||
  110.         (pInfo->m_bDocObject && !(pInfo->m_dwFlags & PRINTFLAG_PROMPTUSER)))
  111.     {
  112.         if (pInfo->m_pPD->m_pd.hDC == NULL)
  113.         {
  114.             // if no printer set then, get default printer DC and create DC without calling
  115.             //   print dialog.
  116.             if (!pApp->GetPrinterDeviceDefaults(&pInfo->m_pPD->m_pd))
  117.             {
  118.                 // bring up dialog to alert the user they need to install a printer.
  119.                 if (!pInfo->m_bDocObject || (pInfo->m_dwFlags & PRINTFLAG_MAYBOTHERUSER))
  120.                     if (pApp->DoPrintDialog(pInfo->m_pPD) != IDOK)
  121.                         return FALSE;
  122.             }
  123.  
  124.             if (pInfo->m_pPD->m_pd.hDC == NULL)
  125.             {
  126.                 // call CreatePrinterDC if DC was not created by above
  127.                 if (pInfo->m_pPD->CreatePrinterDC() == NULL)
  128.                     return FALSE;
  129.             }
  130.         }
  131.  
  132.         // set up From and To page range from Min and Max
  133.         pInfo->m_pPD->m_pd.nFromPage = (WORD)pInfo->GetMinPage();
  134.         pInfo->m_pPD->m_pd.nToPage = (WORD)pInfo->GetMaxPage();
  135.     }
  136.     else
  137.     {
  138.         // otherwise, bring up the print dialog and allow user to change things
  139.         // preset From-To range same as Min-Max range
  140.         pInfo->m_pPD->m_pd.nFromPage = (WORD)pInfo->GetMinPage();
  141.         pInfo->m_pPD->m_pd.nToPage = (WORD)pInfo->GetMaxPage();
  142.  
  143.         if (pApp->DoPrintDialog(pInfo->m_pPD) != IDOK)
  144.             return FALSE;       // do not print
  145.     }
  146.  
  147.     ASSERT(pInfo->m_pPD != NULL);
  148.     ASSERT(pInfo->m_pPD->m_pd.hDC != NULL);
  149.     if (pInfo->m_pPD->m_pd.hDC == NULL)
  150.         return FALSE;
  151.  
  152.     pInfo->m_nNumPreviewPages = pApp->m_nNumPreviewPages;
  153.     VERIFY(pInfo->m_strPageDesc.LoadString(AFX_IDS_PREVIEWPAGEDESC));
  154.     return TRUE;
  155. }
  156. #endif // _WIN32_WCE
  157.  
  158. void CView::OnFilePrint()
  159. {
  160.     // get default print info
  161.     CPrintInfo printInfo;
  162.     ASSERT(printInfo.m_pPD != NULL);    // must be set
  163.  
  164. #if !defined(_WIN32_WCE) 
  165. // WinCE: no printing from shell
  166.     if (LOWORD(GetCurrentMessage()->wParam) == ID_FILE_PRINT_DIRECT)
  167.     {
  168.         CCommandLineInfo* pCmdInfo = AfxGetApp()->m_pCmdInfo;
  169.  
  170.         if (pCmdInfo != NULL)
  171.         {
  172.             if (pCmdInfo->m_nShellCommand == CCommandLineInfo::FilePrintTo)
  173.             {
  174.                 printInfo.m_pPD->m_pd.hDC = ::CreateDC(pCmdInfo->m_strDriverName,
  175.                     pCmdInfo->m_strPrinterName, pCmdInfo->m_strPortName, NULL);
  176.                 if (printInfo.m_pPD->m_pd.hDC == NULL)
  177.                 {
  178.                     AfxMessageBox(AFX_IDP_FAILED_TO_START_PRINT);
  179.                     return;
  180.                 }
  181.             }
  182.         }
  183.  
  184.         printInfo.m_bDirect = TRUE;
  185.     }
  186.  
  187. #endif // _WIN32_WCE
  188.     if (OnPreparePrinting(&printInfo))
  189.     {
  190.         // hDC must be set (did you remember to call DoPreparePrinting?)
  191.         ASSERT(printInfo.m_pPD->m_pd.hDC != NULL);
  192.  
  193.         // gather file to print to if print-to-file selected
  194.         CString strOutput;
  195. #if !defined(_WIN32_WCE) 
  196. // WinCE: no printing to file
  197.         if (printInfo.m_pPD->m_pd.Flags & PD_PRINTTOFILE && !printInfo.m_bDocObject)
  198.         {
  199.             // construct CFileDialog for browsing
  200.             CString strDef(MAKEINTRESOURCE(AFX_IDS_PRINTDEFAULTEXT));
  201.             CString strPrintDef(MAKEINTRESOURCE(AFX_IDS_PRINTDEFAULT));
  202.             CString strFilter(MAKEINTRESOURCE(AFX_IDS_PRINTFILTER));
  203.             CString strCaption(MAKEINTRESOURCE(AFX_IDS_PRINTCAPTION));
  204.             CFileDialog dlg(FALSE, strDef, strPrintDef,
  205.                 OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, strFilter);
  206.             dlg.m_ofn.lpstrTitle = strCaption;
  207.  
  208.             if (dlg.DoModal() != IDOK)
  209.                 return;
  210.  
  211.             // set output device to resulting path name
  212.             strOutput = dlg.GetPathName();
  213.         }
  214.  
  215. #endif // _WIN32_WCE
  216.         // set up document info and start the document printing process
  217.         CString strTitle;
  218.         CDocument* pDoc = GetDocument();
  219.         if (pDoc != NULL)
  220.             strTitle = pDoc->GetTitle();
  221.         else
  222.             GetParentFrame()->GetWindowText(strTitle);
  223.         if (strTitle.GetLength() > 31)
  224.             strTitle.ReleaseBuffer(31);
  225.         DOCINFO docInfo;
  226.         memset(&docInfo, 0, sizeof(DOCINFO));
  227.         docInfo.cbSize = sizeof(DOCINFO);
  228.         docInfo.lpszDocName = strTitle;
  229.         CString strPortName;
  230.         int nFormatID;
  231.         if (strOutput.IsEmpty())
  232.         {
  233.             docInfo.lpszOutput = NULL;
  234.             strPortName = printInfo.m_pPD->GetPortName();
  235.             nFormatID = AFX_IDS_PRINTONPORT;
  236.         }
  237.         else
  238.         {
  239.             docInfo.lpszOutput = strOutput;
  240.             AfxGetFileTitle(strOutput,
  241.                 strPortName.GetBuffer(_MAX_PATH), _MAX_PATH);
  242.             nFormatID = AFX_IDS_PRINTTOFILE;
  243.         }
  244.  
  245.         // setup the printing DC
  246.         CDC dcPrint;
  247.         if (WCE_IF(TRUE,!printInfo.m_bDocObject))
  248.         {
  249.             dcPrint.Attach(printInfo.m_pPD->m_pd.hDC);  // attach printer dc
  250.             dcPrint.m_bPrinting = TRUE;
  251.         }
  252.         OnBeginPrinting(&dcPrint, &printInfo);
  253.  
  254.         if (WCE_IF(TRUE,!printInfo.m_bDocObject))
  255.             dcPrint.SetAbortProc(_AfxAbortProc);
  256.  
  257.         // disable main window while printing & init printing status dialog
  258.         AfxGetMainWnd()->EnableWindow(FALSE);
  259.         CPrintingDialog dlgPrintStatus(this);
  260.  
  261.         CString strTemp;
  262.         dlgPrintStatus.SetDlgItemText(AFX_IDC_PRINT_DOCNAME, strTitle);
  263.         dlgPrintStatus.SetDlgItemText(AFX_IDC_PRINT_PRINTERNAME,
  264.             printInfo.m_pPD->GetDeviceName());
  265.         AfxFormatString1(strTemp, nFormatID, strPortName);
  266.         dlgPrintStatus.SetDlgItemText(AFX_IDC_PRINT_PORTNAME, strTemp);
  267.         dlgPrintStatus.ShowWindow(SW_SHOW);
  268.         dlgPrintStatus.UpdateWindow();
  269.  
  270.         // start document printing process
  271.         if (WCE_IF(TRUE,!printInfo.m_bDocObject) && dcPrint.StartDoc(&docInfo) == SP_ERROR)
  272.         {
  273.             // enable main window before proceeding
  274.             AfxGetMainWnd()->EnableWindow(TRUE);
  275.  
  276.             // cleanup and show error message
  277.             OnEndPrinting(&dcPrint, &printInfo);
  278.             dlgPrintStatus.DestroyWindow();
  279.             dcPrint.Detach();   // will be cleaned up by CPrintInfo destructor
  280.             AfxMessageBox(AFX_IDP_FAILED_TO_START_PRINT);
  281.             return;
  282.         }
  283.  
  284.         // Guarantee values are in the valid range
  285.         UINT nEndPage = printInfo.GetToPage();
  286.         UINT nStartPage = printInfo.GetFromPage();
  287.  
  288.         if (nEndPage < printInfo.GetMinPage())
  289.             nEndPage = printInfo.GetMinPage();
  290.         if (nEndPage > printInfo.GetMaxPage())
  291.             nEndPage = printInfo.GetMaxPage();
  292.  
  293.         if (nStartPage < printInfo.GetMinPage())
  294.             nStartPage = printInfo.GetMinPage();
  295.         if (nStartPage > printInfo.GetMaxPage())
  296.             nStartPage = printInfo.GetMaxPage();
  297.  
  298.         int nStep = (nEndPage >= nStartPage) ? 1 : -1;
  299.         nEndPage = (nEndPage == 0xffff) ? 0xffff : nEndPage + nStep;
  300.  
  301.         VERIFY(strTemp.LoadString(AFX_IDS_PRINTPAGENUM));
  302.  
  303.         // begin page printing loop
  304.         BOOL bError = FALSE;
  305.         for (printInfo.m_nCurPage = nStartPage;
  306.             printInfo.m_nCurPage != nEndPage; printInfo.m_nCurPage += nStep)
  307.         {
  308.             OnPrepareDC(&dcPrint, &printInfo);
  309.  
  310.             // check for end of print
  311.             if (!printInfo.m_bContinuePrinting)
  312.                 break;
  313.  
  314.             // write current page
  315.             TCHAR szBuf[80];
  316.             wsprintf(szBuf, strTemp, printInfo.m_nCurPage);
  317.             dlgPrintStatus.SetDlgItemText(AFX_IDC_PRINT_PAGENUM, szBuf);
  318. #if defined(_WIN32_WCE)
  319.             dlgPrintStatus.UpdateWindow();
  320.             if(TRUE)
  321. #else
  322.             if (!printInfo.m_bDocObject)
  323. #endif
  324.             {
  325.                 // set up drawing rect to entire page (in logical coordinates)
  326.                 printInfo.m_rectDraw.SetRect(0, 0,
  327.                     dcPrint.GetDeviceCaps(HORZRES),
  328.                     dcPrint.GetDeviceCaps(VERTRES));
  329. #if !defined(_WIN32_WCE)  
  330. // WinCE: we just keep m_rectDraw in client coords (MM_TEXT)
  331. // set up drawing rect to entire page (in logical coordinates)
  332.                 dcPrint.DPtoLP(&printInfo.m_rectDraw);
  333. #endif // _WIN32_WCE
  334.  
  335.                 // attempt to start the current page
  336.                 if (dcPrint.StartPage() < 0)
  337.                 {
  338.                     bError = TRUE;
  339.                     break;
  340.                 }
  341.  
  342.                 // must call OnPrepareDC on newer versions of Windows because
  343.                 // StartPage now resets the device attributes.
  344.                 if (afxData.bMarked4)
  345.                     OnPrepareDC(&dcPrint, &printInfo);
  346.             }
  347.  
  348.             ASSERT(printInfo.m_bContinuePrinting);
  349.  
  350.             // page successfully started, so now render the page
  351.             OnPrint(&dcPrint, &printInfo);
  352.             if (WCE_IF(TRUE,!printInfo.m_bDocObject) &&
  353.                 (dcPrint.EndPage() < 0 || !_AfxAbortProc(dcPrint.m_hDC, 0)))
  354.             {
  355.                 bError = TRUE;
  356.                 break;
  357.             }
  358.         }
  359.  
  360.         // cleanup document printing process
  361.         if (WCE_IF(TRUE,!printInfo.m_bDocObject))
  362.         {
  363.             if (!bError)
  364.                 dcPrint.EndDoc();
  365.             else
  366.                 dcPrint.AbortDoc();
  367.         }
  368.  
  369.         AfxGetMainWnd()->EnableWindow();    // enable main window
  370.  
  371.         OnEndPrinting(&dcPrint, &printInfo);    // clean up after printing
  372.         dlgPrintStatus.DestroyWindow();
  373.  
  374.         dcPrint.Detach();   // will be cleaned up by CPrintInfo destructor
  375.     }
  376. }
  377.  
  378. /////////////////////////////////////////////////////////////////////////////
  379. // CPrintInfo helper structure
  380.  
  381. CPrintInfo::CPrintInfo()
  382. {
  383.     m_pPD = new CPrintDialog(FALSE, PD_ALLPAGES | PD_USEDEVMODECOPIES |
  384.         PD_NOSELECTION);
  385.  
  386.     ASSERT(m_pPD->m_pd.hDC == NULL);
  387.  
  388.     SetMinPage(1);              // one based page numbers
  389.     SetMaxPage(0xffff);         // unknown how many pages
  390.  
  391.     m_nCurPage = 1;
  392.  
  393.     m_lpUserData = NULL;        // Initialize to no user data
  394. WCE_DEL m_bPreview = FALSE;         // initialize to not preview
  395. WCE_DEL m_bDirect = FALSE;          // initialize to not direct
  396. WCE_DEL m_bDocObject = FALSE;       // initialize to not IPrint
  397.     m_bContinuePrinting = TRUE; // Assume it is OK to print
  398.  
  399. WCE_DEL m_dwFlags = 0;
  400. WCE_DEL m_nOffsetPage = 0;
  401. }
  402.  
  403. CPrintInfo::~CPrintInfo()
  404. {
  405.     if (m_pPD != NULL && m_pPD->m_pd.hDC != NULL)
  406.     {
  407.         ::DeleteDC(m_pPD->m_pd.hDC);
  408.         m_pPD->m_pd.hDC = NULL;
  409.     }
  410.     delete m_pPD;
  411. }
  412.  
  413. /////////////////////////////////////////////////////////////////////////////
  414. #endif // _WIN32_WCE_NO_PRINTING
  415.